<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

require_once(dirname(__FILE__).'/../lib/BasesfGuardAuthActions.class.php');

/**
 *
 * @package    symfony
 * @subpackage plugin
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: actions.class.php 23319 2009-10-25 12:22:23Z Kris.Wallsmith $
 */
class sfGuardAuthActions extends BasesfGuardAuthActions
{
	public function executeSignin($request)
	{
		$this->setLayout('2columns-right');
		parent::executeSignin($request);
	}

	public function executeSignout($request)
	{
		$this->getUser()->signOut();
		$this->redirect('@homepage');
	}

	public function executeSendmessage(sfWebRequest $request)
	{
		$this->form = new PasswordRequestForm();
		$email = $request->getParameter('email');
		$user = sfGuardUser::GetUserByEmail($email);

		if(!$user)
			$email='no such email';
			
		$this->form->bind(array(
				'email'=> $email,
				'_csrf_token'=> $request->getParameter('_csrf_token'),
		));

		if($this->form->isValid())
		{
			// send new password by e-mail
			$newpass = $this->randomPassword();
			$user->setPassword($newpass);
			$user->save();
			$message = $this->getMailer()->compose(
				array('no-reply@iplife.com.ua'=> 'Iplife Bot'),
				$email,
				'Восстановление пароля на сайте iplife.com.ua',
				'Здравствуйте, '.$user->getFirstName().'! Ваш новый пароль: '.$newpass
				);
				$this->getMailer()->send($message);
			$this->getUser()->setAttribute('messagesend', 'Пароль отправлен Вам по электронной почте');
			$this->redirect('default/index');
		}
		else
		$this->setTemplate('password');
	}

	protected function randomPassword( $allow = "a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9")
	{
		$array = explode(' ', $allow);
		shuffle($array);
		$allow = implode($array);
		$ret = substr($allow, 5, 8);
		return $ret;
	}
}